summaryrefslogtreecommitdiff
path: root/src/explicitsimulation.cpp
blob: 281e7e59b8324ccf2b4dd4be8ae99b5ba2ad6446 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "genetic/explicitsimulation.h"
#include "genetic/operator.h"
#include "genetic/fitnessfunction.h"

#include <bu/random.h>
#include <bu/sio.h>

using namespace Bu;

Genetic::ExplicitSimulation::ExplicitSimulation( Genetic::Operator *pOper,
		Genetic::FitnessFunction *pFunc, int iPopSize, float fKeep,
		float fRandom, bool bKeepBest ) :
	pOper( pOper ),
	pFunc( pFunc ),
	iPopSize( iPopSize ),
	fKeep( fKeep ),
	fRandom( fRandom ),
	bKeepBest( bKeepBest )
{
	for( int j = 0; j < iPopSize; j++ )
	{
		xPop.addPhenotype( pOper->random() );
	}

	updateFitness();
}

Genetic::ExplicitSimulation::~ExplicitSimulation()
{
	delete pOper;
	delete pFunc;
}

void Genetic::ExplicitSimulation::timestep()
{
	PhenotypeList lNew;
	
	int iChildren = iPopSize*(1.0-fKeep-fRandom);

	// Create children
	for( int j = 0; j < iChildren; j++ )
	{
		PhenotypeList lParents;
		for( int k = 0; k < pOper->parentCount(); k++ )
			lParents.append( xPop.getPhenotype( selectWeighted() ) );
		lNew.append( pOper->mate( lParents ) );
	}

	// Select phenotypes for keeping
	int iKeep = iPopSize*fKeep;
	FitnessHash hTempFitness;
	for( int j = 0; j < iKeep; j++ )
	{
		Genetic::PhenotypeId id = selectWeighted();
		lNew.append( xPop.takePhenotype( id ) );
		hTempFitness.insert( id, hFitness.get( id ) );
		hFitness.erase( id );
		dTotalFitness -= hTempFitness.get( id );
	}

	if( bKeepBest && hFitness.has( uMaxFitness ) )
	{
		lNew.append( xPop.takePhenotype( uMaxFitness ) );
		hTempFitness.insert( uMaxFitness, hFitness.get( uMaxFitness ) );
		hFitness.erase( uMaxFitness );
		dTotalFitness -= hTempFitness.get( uMaxFitness );
	}

	// Fill in the remainder with random phenotypes
	while( lNew.getSize() < iPopSize )
	{
		lNew.append( pOper->random() );
	}

	// Refill the population
	hFitness = hTempFitness;
	xPop.clear();
	xPop.timestep();
	for( PhenotypeList::iterator i = lNew.begin(); i; i++ )
	{
		xPop.addPhenotype( *i );
	}

	updateFitness();
}

Genetic::PhenotypeId Genetic::ExplicitSimulation::selectWeighted()
{
	double dSel = Bu::Random::randNorm()*dTotalFitness;
	double dRun = 0.0;

	for( FitnessHash::iterator i = hFitness.begin(); i; i++ )
	{
		dRun += *i;
		if( dSel < dRun )
			return i.getKey();
	}

	sio << "Genetic::ExplicitSimulation::selectWeighted() - failed, picked max"
		<< sio.nl;

	return uMaxFitness;
}

void Genetic::ExplicitSimulation::updateFitness()
{
	dMinFitness = -1.0;
	dTotalFitness = 0.0;
	for( Population::iterator i = xPop.begin(); i; i++ )
	{
		double dFitness;
		if( hFitness.has( i.getKey() ) )
		{
			dFitness = hFitness.get( i.getKey() );
		}
		else
		{
			dFitness = (*pFunc)( *i );
			if( dFitness < 0.0 )
				dFitness = 0.0;
			hFitness.insert( i.getKey(), dFitness );
		}
		dTotalFitness += dFitness;
		if( dMinFitness < 0.0 )
		{
			dMinFitness = dMaxFitness = dFitness;
			uMaxFitness = i.getKey();
		}
		else if( dMinFitness > dFitness )
		{
			dMinFitness = dFitness;
		}
		else if( dMaxFitness < dFitness )
		{
			dMaxFitness = dFitness;
			uMaxFitness = i.getKey();
		}
	}
}